<?php 
## include required files
/*******************************/
require_once '../../includefiles.php';
require_once '../../model/users.php';
include_once("../../model/trade.php");
require_once "../../includes/classes/paging_htacess.php";
require_once '../../redirect.php';

## creating objects of required models
$userObj 		= new Model_Users();
$tradeObj = new Model_TRADE();

checkUserLogin();

if($_SESSION['user_type'] == '3')
{
	@header("location:".SITE_URL."/");
	exit;
}


$userId =$_SESSION['getbids_id'];

## When user wants to delete the testimonial
if($_GET['id1'] == 'delete' && $_GET['id2']!='')
{
	$tradeID = base64_decode($_GET['id2']);	
	$tradeObj->deleteTradeCategoryById($tradeID);
	$_SESSION['msg'] = "<div class='success_msg'style='margin-top:5px;margin-bottom: 20px;'><span>Trade category deleted successfully!</span></div>";		
	header("Location:".SITE_URL."/myaccount/mytradecategory");
	exit;
	
}

## When user wants to delete the testimonial
if($_GET['id3'] == 'delete' && $_GET['id4']!='')
{
		$jobtyypeId = base64_decode($_GET['id4']);	
		$tradeObj->deleteJobTypeById($jobtyypeId);
		$_SESSION['msg'] = "<div class='success_msg'style='margin-top:5px;margin-bottom: 20px;'><span>Job Type deleted successfully!</span></div>";
		header("Location:".SITE_URL."/myaccount/".$_GET['id1']."/".$_GET['id2']."/mytradecategory");
	exit;
}


## for pagination
	$page = $_GET['id1'];
	if(!isset($_GET['id1']))
	$page = 1;
	else
	$page = $page;
	
## limit per pag 
$tetsimonialPerPage =10;
$StartRow = $tetsimonialPerPage  * ($page-1);
## Getting all myaccount from database according to user

if($_POST['search']!='' && $_POST['search']!='Search')
{
	$search = addslashes($_POST['search']); 
} 
elseif($_GET['id3']!='')
{
	$search =addslashes(base64_decode($_GET['id3'])); 
}
else {
	$search = '';
}
$smarty -> assign("search",stripslashes (stripslashes ($search)));
if($_GET['id2']!='' && $_GET['id2']!='parent')
{
	$pid = base64_decode($_GET['id2']);	
}
else
	$pid =0;

$tradeArray  = $tradeObj->getAllTradesCategoryByUserId($userId,$pid,$search); 
$totalCount        = count($tradeArray);
$smarty -> assign("recordsFound",$totalCount);

$tradeArray = $tradeObj->getAllTradesCategoryByUserId($userId,$pid,$search,$tetsimonialPerPage,$StartRow);
$show = 1;
$total_pages = ceil($totalCount / $tetsimonialPerPage);
	if($total_pages > 1)
	$smarty -> assign("showpgnation","yes");
	$showing    = !isset($_GET["id1"]) ? 1 : $page;
	
	## it shoould be file name 
	if($_GET['id2']!='' && $_GET['id2']!='parent')
	{
		if($search!='')
			$firstlink  = $_GET['id2']."/".base64_encode($search)."/mytradecategory/";
		else
			$firstlink  = $_GET['id2']."/mytradecategory/";	
	}
	else
	{
		if($search!='')
			$firstlink  = "parent/".base64_encode($search)."/mytradecategory/";
		else
			$firstlink  = "mytradecategory/";	
	
	}
	
	$baselink   = $firstlink; 
	if($total_pages!=0)
		$pagenation = pagination($total_pages, $show, $showing, $firstlink, $baselink, "", "myaccount");
	$smarty -> assign("pagenation", $pagenation);
	$smarty -> assign("page", $page);
	
	for($i=0;$i<count($tradeArray);$i++)
	{	
		if($_GET['id2']=='' || $_GET['id2']=='parent')
		{	
			$parentArray=$tradeObj->getCountJobtypeByTradeId($tradeArray[$i]['id']);		
			$tradeArray[$i]['cntjobtype'] =$parentArray;
		}
	}
	
	//echo "<pre>"; print_r($tradeArray);exit;
	$smarty->assign("tradeArray", $tradeArray);
	
/*===========================================================================*/
	$userDetArray = $userObj->getUserDetailsByUserId($userId);
	$smarty->assign("userDetArray", $userDetArray);	
	
/*===========================================================================*/	
if($_GET['id2']!='' && $_GET['id2']!='parent')
{
	$pid= base64_decode($_GET['id2']); 
	$parentArray=$tradeObj->getTradeCategoryDetailsById($pid);
	$smarty->assign('parentArray', $parentArray);
	//echo '<pre>';print_r($moduelArray);exit;
}

if($_SESSION['msg']){
		$smarty->assign("msg", $_SESSION['msg']);
		unset($_SESSION['msg']);
}


$active = "myaccount";
$smarty->assign("active", $active);
$subactive ="mytradecategory";
$smarty->assign("subactive", $subactive);
$smarty->assign('mainmenu', '3');
$smarty->display(TEMPLATEDIR . '/modules/myaccount/mytradecategory.tpl');
?>